package com.joejag.mavenstats.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.widgets.QuickTips;
import com.gwtext.client.widgets.form.Field;
import com.joejag.mavenstats.client.dto.ProjectDTO;
import com.joejag.mavenstats.client.dto.ReportDTO;
import com.joejag.mavenstats.client.service.ProjectsService;
import com.joejag.mavenstats.client.service.ReportsService;
import com.joejag.mavenstats.client.views.main.home.HomePage;
import com.joejag.mavenstats.client.views.main.projects.DependencyDetail;
import com.joejag.mavenstats.client.views.main.reports.ReportView;
import com.joejag.mavenstats.client.views.north.TopPanel;
import com.joejag.mavenstats.client.views.sidebar.SideBar;

import java.util.ArrayList;
import java.util.List;


public class MavenStats implements EntryPoint, WindowResizeListener
{
  private static MavenStats singleton;

  private static final Images images = (Images) GWT.create(Images.class);

  public interface Images extends SideBar.Images, TopPanel.Images
  {
  }

  public static MavenStats instance()
  {
    return singleton;
  }

  private TopPanel topPanel = new TopPanel(images);
  private VerticalPanel rightPanel = new VerticalPanel();

  private DependencyDetail dependencyDetail = new DependencyDetail();
  private HomePage homepage = new HomePage();
  private ReportView reportView = new ReportView();

  private SideBar sideBar = new SideBar(images);

  public void onModuleLoad()
  {
    singleton = this;

    Field.setMsgTarget("side");
    QuickTips.init();

    topPanel.setWidth("100%");
    rightPanel.add(homepage);
    dependencyDetail.setWidth("100%");

    DockPanel outer = new DockPanel();
    outer.add(topPanel, DockPanel.NORTH);
    outer.add(sideBar, DockPanel.WEST);
    outer.add(rightPanel, DockPanel.CENTER);
    outer.setWidth("100%");
    outer.setSpacing(4);
    outer.setCellWidth(rightPanel, "100%");

    Window.addWindowResizeListener(this);
    Window.enableScrolling(false);
    Window.setMargin("0px");

    RootPanel.get().add(outer);

    DeferredCommand.addCommand(new Command()
    {
      public void execute()
      {
        onWindowResized(Window.getClientWidth(), Window.getClientHeight());
      }
    });

    onWindowResized(Window.getClientWidth(), Window.getClientHeight());
  }

  public void onWindowResized(int width, int height)
  {
    int shortcutHeight = height - sideBar.getAbsoluteTop() - 8;
    sideBar.setHeight("" + (shortcutHeight < 1 ? 1 : shortcutHeight));

    dependencyDetail.adjustSize(width, height);
    homepage.adjustSize(width, height);
    reportView.adjustSize(width, height);
  }

  public void showHomepage()
  {
    showView(MavenStats.this.homepage);
  }

  public void refreshProjectsTree()
  {
    sideBar.refreshProjectsTree(images);
  }

  public void displayProject(final ProjectDTO dependency)
  {
    ProjectsService.App.getInstance().getDependencies(dependency.projectURL, new AsyncCallback()
    {
      public void onFailure(Throwable throwable)
      {
        dependencyDetail.setItem(dependency, new ArrayList());
      }

      public void onSuccess(Object o)
      {
        dependencyDetail.setItem(dependency, (List) o);
        showView(MavenStats.this.dependencyDetail);
      }
    });
  }

  public void displayReport(final String reportName)
  {
    ReportsService.App.getInstance().getReport(reportName, new AsyncCallback()
    {
      public void onFailure(Throwable throwable)
      {
      }

      public void onSuccess(Object o)
      {
        reportView.setReport((ReportDTO) o);
        showView(MavenStats.this.reportView);
      }
    });
  }

  private void showView(Composite reportView)
  {
    rightPanel.remove(rightPanel.getWidget(0));
    rightPanel.add(reportView);
    onWindowResized(Window.getClientWidth(), Window.getClientHeight());
  }
}
